package com.nifostasky.comic.business.controller;

import com.nifostasky.comic.service.CoHistoryRecordService;
import com.nifostasky.comic.service.CoHomepageInfoService;
import com.nifostasky.comic.service.CoSpiderInfoService;
import com.nifostasky.comic.vos.JsonResult;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务
 */
@RestController
@RequestMapping("/scheduledTaskController")
@Api(tags = "定时任务")
@Slf4j
@ApiSort(9999)
@AllArgsConstructor
public class ScheduledTaskController {
    private CoHomepageInfoService coHomepageInfoService;
    private CoSpiderInfoService coSpiderInfoService;
    private CoHistoryRecordService coHistoryRecordService;

    /**
     * 每日凌晨4点获取所有python文件 今日的最新漫画信息
     */
    @Scheduled(cron = "0 0 4 * * ?")
    @ApiOperation(value = "定时获取最新漫画信息",notes = "每日凌晨4点获取所有python文件 今日的最新漫画信息")
    @GetMapping("/saveTodayComicInfo")
    @ApiOperationSupport(order = 1)
    @SneakyThrows
    public JsonResult saveTodayComicInfo(){
        List<Long> pyIds = coSpiderInfoService.list().stream().map(i -> i.getId()).collect(Collectors.toList());
        for (Long pyId : pyIds){
            coHomepageInfoService.saveTodayComicInfo(pyId);
            Thread.sleep(5000);
        }
        return new JsonResult(true);
    }

//
//    /**
//     * 每日凌晨4点 删除用户7天前的历史记录
//     * @return
//     */
//    @Scheduled(cron = "0 10 4 * * ?")
//    @ApiOperation(value = "删除用户历史记录",notes = "每日凌晨4点 删除用户7天前的历史记录")
//    @GetMapping("/deleteDateRange7")
//    @ApiOperationSupport(order = 1)
//    public JsonResult deleteDateRange7(){
//        Integer integer = coHistoryRecordService.deleteDateRange7();
//        return new JsonResult(integer);
//    }
}
